package com.glhtgyxy.biyesheji.Mapper;

import com.glhtgyxy.biyesheji.Pojo.evaluate;
import com.glhtgyxy.biyesheji.Pojo.house;
import com.glhtgyxy.biyesheji.Pojo.merchant;
import com.glhtgyxy.biyesheji.Pojo.user;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Mapper
public interface clientMapper {

    @Select("select * from house where id = #{id}")
    ArrayList<house> findHouseById(int id);


    @Select("select * from house where id = #{id}")
    int houseIdFindMaser(int id);

    @Select("select * from merchant where id = #{id}")
    ArrayList<merchant> masterIdFindMerchant(int id);

    @Select("select * from evaluate where merchantid = #{id}")
    ArrayList<evaluate> merchantIdFindEvaluate(int id);

    @Insert("insert into `order` (houseid,userid,checkInDate,checkOutDate) values (#{houseid},#{userid},#{checkInDate},#{checkOutDate})")
    int tijiaodingdan(int houseid,int userid,String checkInDate,String checkOutDate);




    @Select("select userid from evaluate where merchantid = #{id}")
    int[] evaFindUser(int id);


    @Select("select * from user where id = #{id}")
    user finduser(int id);


    @SelectProvider(type = HouseSqlProvider.class, method = "findHouse")
    ArrayList<house> findHouse(HashMap<String, String> map);

    public class HouseSqlProvider{
        public String findHouse(HashMap<String, String> map) {
            StringBuilder sql = new StringBuilder("SELECT * FROM house WHERE 1=1 ");

            if (map.containsKey("addr") && map.get("addr") != null) {
                sql.append("AND addr like concat('%',#{addr},'%')");
            }

            if (map.containsKey("pricelow") && map.containsKey("priceheigh")) {
                sql.append("AND price BETWEEN #{pricelow} AND #{priceheigh} ");
            } else if (map.containsKey("pricelow")) {
                sql.append("AND price >= #{pricelow} ");
            } else if (map.containsKey("priceheigh")) {
                sql.append("AND price <= #{priceheigh} ");
            }

            if (map.containsKey("bedroom") && map.get("bedroom") != null) {
                sql.append("AND bedroom like concat('%',#{bedroom},'%') ");
            }
            if (map.containsKey("search") && map.get("search") != null) {
                sql.append("AND (addr like concat('%',#{search},'%') or price like concat('%',#{search},'%') or bedroom like concat('%',#{search},'%') or housename like concat('%',#{search},'%') or id like concat('%',#{search},'%') or facilityservices like concat('%',#{search},'%') ) ");
            }

            return sql.toString();
        }
    }
}